//
//  JXLEmoticonTools.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/10/8.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

//读取表情工具类
import UIKit
//每页显示的表情个数
let itemNumberOfPage = 20
class JXLEmoticonTools: NSObject {

    //单例
    static let sharedTools: JXLEmoticonTools = JXLEmoticonTools()
    
    //构造函数私有化，这样外界不可以通过类型加（）的方式创建类的对象
    private override init() {
        super.init()
        
    }
    
    //bundle对象
    let emoticonBundle: Bundle = {
        
        //  获取emoticon.bundle的路径
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        
        //  通过路径创建emoticonbundle对象
        let bundle = Bundle(path: path)!
        return bundle
    }()
    
    //给表情视图提供的数据结构
    lazy var allEmoticonArray: [[[JXLEmoticon]]] = {
       
        return [
        [self.recentEmoticonArray],
         self.sectionEmoticonArray(emoticonArray: self.defaultEmoticonArray),
         self.sectionEmoticonArray(emoticonArray: self.emojiEmoticonArray),
         self.sectionEmoticonArray(emoticonArray: self.lxhEmoticonArray),
        ]
    }()
    
    //获取最近表情数据
    lazy var recentEmoticonArray: [JXLEmoticon] = {
        let array = [JXLEmoticon]()
        
        return array
    }()
    
    //获取默认表情数据
    lazy var defaultEmoticonArray: [JXLEmoticon] = {
        
      return self.loadEmoticonsArray(folderName: "default", fileName: "info.plist")
    }()
    
    //获取emoji表情数据
    lazy var emojiEmoticonArray: [JXLEmoticon] = {
        
        return self.loadEmoticonsArray(folderName: "emoji", fileName: "info.plist")
    }()
    
    //获取浪小花表情数据
    lazy var lxhEmoticonArray: [JXLEmoticon] = {
        
        return self.loadEmoticonsArray(folderName: "lxh", fileName: "info.plist")
    }()
    
    //  读取表情数据
    private func loadEmoticonsArray(folderName: String, fileName: String) -> [JXLEmoticon] {
        //  self.emoticonsBundle.path(forResource: xxx)  可以透过两次文件夹 (Contents/Resources)
        //  获取info.plist文件路径
        let subPath = folderName + "/" + fileName
        let path = self.emoticonBundle.path(forResource: subPath, ofType: nil)!
        
        //获取资源数据
        let dicArray = NSArray(contentsOfFile: path) as! [[String: Any]]
        
        //  使用yymodel把字典转成模型
        let modelArray = NSArray.yy_modelArray(with: JXLEmoticon.self, json: dicArray) as! [JXLEmoticon]
        
        //  遍历数组模型，判断如果是图片类型，需要拼接路径
        for model in modelArray {
            
            if model.type == "0" {
                //设置图片全路径
                model.path = self.emoticonBundle.path(forResource: folderName, ofType: nil)! + "/" + model.png!
               //设置图片对应的文件夹名称
                model.folderName = folderName
            }
        }
        return modelArray

    }
    
    //拆分二维数组
    func sectionEmoticonArray(emoticonArray: [JXLEmoticon]) -> [[JXLEmoticon]] {
        //根据数组个数计算显示的页数
        let pageCount = (emoticonArray.count - 1) / itemNumberOfPage + 1
        
        //创建一个可变二维数组，用于保存拆分好的数组
        var tempArray: [[JXLEmoticon]] = [[JXLEmoticon]]()
        
        for i in 0..<pageCount {
            
            //开始截取的索引
            let loc = i * itemNumberOfPage
            //截取的长度
            var len = itemNumberOfPage
            
            //表示数组越界
            if loc + len > emoticonArray.count {
              //获取截取剩余个数
                len = emoticonArray.count - loc
            }
            
            let subArray = (emoticonArray as NSArray).subarray(with: NSMakeRange(loc, len)) as! [JXLEmoticon]
            
            tempArray.append(subArray)
        }
        
        return tempArray
    }

}
